package exhi.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.stp.core.entity.IGridRowFactory;
import com.stp.core.entity.XGrid;
import com.stp.core.entity.XGridPage;
import com.stp.core.entity.XGridRow;
import com.stp.core.entity.XMsg;
import com.stp.core.spring3.BaseSpringAction;

import exhi.entity.EntpEx2co;
import exhi.manager.EntpEx2coMng;

@Controller
@RequestMapping("/entp")
public class EntpEx2coAct extends BaseAct {
	
	private static final String PATH_TABLE = "/ex2co-table";
	private static final String PATH_LIST = "/ex2co-list";
	private static final String PATH_DETAIL = "/ex2co-detail";
	private static final String PATH_SAVE = "/ex2co-save";
	private static final String PATH_DELETE = "/ex2co-delete";
	
	// 企业评价场馆 
	@RequestMapping(PATH_TABLE)
	public void table(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			ModelMap model) {
		model.addAttribute("code", code);

	}
	
	// 权限json数据
	@RequestMapping(PATH_LIST)
	@ResponseBody
	public String infoList(XGridPage page,
			@RequestParam("cap_code") String capCode,
			@RequestParam("cap_name") String capName)
			throws InstantiationException, IllegalAccessException {

		String sql = "from EntpEx2co info where 1=1";
		sql += sqlModel.getSQLChars("info.code", "=", capCode, false, "and");
		sql += sqlModel.getSQLChars("info.dwmc", "like", capName, false,
				"like");
		sql += " order by info.createtime desc";
		InfoGridRow row = new EntpEx2coAct.InfoGridRow();
		XGrid grid = entpEx2coMng.getXGrid(sql, row, page.getPageNo(),
				page.getPageSize(), false);
		if (null == grid)
			return "";
		return "{\"xGrid\":" + toJson(grid) + "}";
	}
	
	// 明细数据
	@RequestMapping(PATH_DETAIL)
	public void infoDetail(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			@ModelAttribute("entpEx2co") EntpEx2co entpEx2coInfo,
			ModelMap model) {
		model.addAttribute("code", code);
		model.addAttribute("info", entpEx2coInfo);

	}
	
	// 数据保存
	@RequestMapping(PATH_SAVE)
	@ResponseBody
	public String saveInfo(
			HttpServletRequest request,
			@ModelAttribute("entpEx2co") EntpEx2co entpEx2coInfo,
			ModelMap model) throws IllegalStateException, IOException {
		entpEx2coInfo.setCreatetime(new Date());
		entpEx2coMng.save(entpEx2coInfo);
		XMsg msg = new XMsg();
		String entpId = entpEx2coInfo.getId();
		
		msg.setState(true);
		return "redirect:" + PATH_TABLE.substring(1);
	}
	
	@RequestMapping(PATH_DELETE)
	@ResponseBody
	public String infoDelete(
			@ModelAttribute("entpEx2co") EntpEx2co entpEx2coInfo,
			ModelMap model) {
		XMsg msg = new XMsg();
		msg.setState(true);
		entpEx2coMng.delete(entpEx2coInfo);
		return toJson(msg);
	}
	
	@ModelAttribute("entpEx2co")
	public EntpEx2co getEntpSexhiInfo(
			@RequestParam(value = "infoId", required = true, defaultValue = "") String id) {
		EntpEx2co entpEx2co = entpEx2coMng.get(id);
		if (null == entpEx2co) {
			entpEx2co = new EntpEx2co();
		}
		return entpEx2co;
	}
	
	class InfoGridRow implements IGridRowFactory {
		public XGridRow builderXRow(Object obj) {
			// TODO Auto-generated method stub
			XGridRow xrow = null;
			if (obj instanceof EntpEx2co) {
				xrow = new XGridRow();
				EntpEx2co info = (EntpEx2co) obj;
				xrow.setId(info.getId());
				List t = new ArrayList();
				t.add("");
				t.add(info.getDwmc());
				t.add(info.getJfqk());
				t.add(info.getSfyz());
				t.add("<a href='ex2co-detail.jspx?infoId=" + info.getId()
						+ "&code=" + info.getCode() + "'>【编辑】</a>");
				xrow.setData(t);
			}
			return xrow;
		}
	}

	
	@Autowired
	@Qualifier("entpEx2coMng")
	private EntpEx2coMng entpEx2coMng;
}
